Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

errorlog

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

errorlog

Yet another logger for Node

  • 1.4.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
38
decreased by-46.48%
Maintainers
1
Weekly downloads
 
Created
Source

Simple Error Log

A very simple error logger/formatter for Node.

  • Install and use
  • Options and properties
  • Logging
  • License (MIT)

Install and use

Install as usual with NPM:

npm install --save errorlog

Then configure as the last route of your Express app:

// Get our creator
var errorlog = require('errorlog');
// Create a new logger (with options!)
var log = errorlog(...);
// Foo! Log messages, errors, ...
log('My log message');

Options and properties

The error log can be constructed with the following options:

  • logger may be one of the following:
    • a Writable stream to which error messages will be written to (actually an object offering a write(...) function will do).
    • a simple function that will be invoked once with each message to log.
    • if unspecified this will default to process.stderr.
  • category: a category name that will be inserted in the message to log.
  • level: the minimum level to log messages at
    • errorlog.TRACE or 0: really low priority messages.
    • errorlog.DEBUG or 100: debugging messages.
    • errorlog.INFO or 200: informational messages (default).
    • errorlog.WARN or 300: warning messages.
    • errorlog.ERROR or 400: error messages.
    • errorlog.FATAL or 500: fatel error messages.
    • errorlog.ALL or any number smaller than 100: everything is logged.
    • errorlog.OFF or any number greater than 500: disable logging.

Options can be specified at construction wrapped in a simple object:

require('errorlog')({
  logger: process.stdout,
  category: 'my category',
  level: 200
});

Or can be specified as a single parameter (either a Writable stream, a function, or a string identifying the category):

// Forward errors to Winston's "error" facility
var winston = require('winston');
var errorlog = require('errorlog');

// Pass "winston.error" as a writer function
var log = errorlog(winston.error);
Default Logger and Level

The default Writable stream or logging function can be configured using the defaultLog property:

var errorlog = require('errorlog');
errorlog.defaultLog = process.stdout;
errorlog.defaultLevel = errorlog.DEBUG;

// All logs will use `process.stdout` unless overridden
var log = errorlog('my category');
log('A message to log');

The default for output will be process.stderr and minimum level ERROR: these values will be applied only to loggers created after setting them.

The default level can also be specified by using the LOG_LEVEL environment variable.

Logging

Needless to say, logging follow the standard util.format scheme:

log.debug('I have %d %s, and an object %j', 3, 'apples', { foo: 'bar' });

This will produce an entry like

2015-03-31T13:58:06.601Z - DEBUG - I have 3 apples and an object {"foo":"bar"}
Levels

Logging levels are associated to various functions of the logger, for example:

var errorlog = require('errorlog');
var log = errorlog({...});

log('Default, logged unless everything is OFF');
log.debug('A debug message');
log.info('Informational message');
log.warn('Some sort of warning');
log.error('Like calling log(...)');
Extras

Extra parameters that do not match format characters will be logged in their JSON format, and errors will have their stack traces logged, too. For example:

var extra = { key: 'a simple value' };
var error = new Error('This is an error');
log.error('I have %d %s', 3, 'apples', extra, error);

Will produce something like

2015-03-30T16:45:01.718Z - ERROR - I have 3 apples
  >>> {"key":"a simple value"}
  Error: This is an error
    at Error (native)
    at ... stacktrace continues ...
Colorization

If the configured log is either process.stderr (the default) or process.stderr, the output to the console will be colorized.

And now I wish GitHub didn't filter the style attribute...

This behavior can be changed by setting the errorlog.defaultColorize property to false.

var errorlog = require('errorlog');
errorlog.defaultColorize = false;

log('There you go, some bland output...');

To get an idea of the output, simply run the sample.js included here.

Log rotation

For log rotation errorlog plays nicely with packages like logrotate-stream.

License (MIT)

Copyright (c) 2015 USRZ.com and Pier Paolo Fumagalli

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Keywords

FAQs

Package last updated on 29 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc